package com.bbzn.device.client.service.emq.data;

import com.alibaba.fastjson.JSONObject;
import com.bbzn.device.client.dataobject.Device;
import com.bbzn.device.client.service.DeviceService;
import com.bbzn.device.client.service.EmqStrategy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author: wulongbo
 * @Date : 2021/4/9 11:09
 * @Version 1.0
 * 设置定位数据上报时间间隔
 */
@Slf4j
@Component
public class EmqVltageStrategy implements EmqStrategy {

    @Autowired
    DeviceService deviceService;

    @Override
    public String emqAction(String payLoad) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(payLoad);
            String imei = jsonObject.getString("imei");
            if (StringUtils.isEmpty(imei)) {
                return "imei错误！卡号不能为空！";
            }
            //更新近电上报阈值状态
            Device device = new Device();
            device.setImei(imei);
            // 设置成功
            device.setVoltageState(0);
            deviceService.updateByImeiSelective(device);
        } catch (Exception e) {
            e.printStackTrace();
            return "设置设备近电失败！";
        }
        return "设置设备近电成功！";
    }
}
